package com.Servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jdbc.Jdbc;

/**
 * Servlet implementation class remove_checkdate_final
 */
@WebServlet("/remove_checkdate_final")
public class remove_checkdate_final extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public remove_checkdate_final() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String classname = request.getParameter("classname");
		String centername = request.getParameter("centername");
		String date = request.getParameter("datecheck");
		System.out.println(classname.length());
		// alter table 表名 drop column 列名
		String table = centername + classname + "check ";

		// 找到删除这天所有学生签到的状态
		String find = "select stuid," + date + " from " + table + "";
		Jdbc f = new Jdbc();
		try {
			f.Jdbc_Q2(find);
			while (f.rs.next()) {
				if (f.rs.getString(date) != null) {
					String stuid = f.rs.getString("stuid");
					String updatet = "update " + table + " set total=total-1 where stuid='" + stuid + "'";
					Jdbc updatetotal = new Jdbc(updatet);
					if (f.rs.getString(date).equals("1") || f.rs.getString(date).equals("2")) {
						String updatea = "update " + table + " set absence=absence-1 where stuid='" + stuid + "'";
						Jdbc updateabsence = new Jdbc(updatea);
						String date1 = date.replace("日", "");
						date1 = date1.replaceAll("年|月", "-");
						// 先从checkdate中获取删除那天的课时数，然后将其获得并且重新加载到contract表中去
						// 并且从checktable中获取stuid就是contract中的id
						String findclasstime = "select classtime from checkdate where classname='" + classname
								+ "' and centername='" + centername + "' and date='" + date1 + "' ";
						String classtime = null;
						System.out.println(findclasstime);
						Jdbc fct = new Jdbc();
						ResultSet rsid = null;
						try {
							fct.Jdbc_Q2(findclasstime);
							fct.rs.next();
							classtime = fct.rs.getString("classtime");
							System.out.println("classtime:"+classtime);
							String update = "update contract set restclass = restclass+" + classtime + " where id='"
									+ stuid + "' ";
							System.out.println("update:" + update);
							Jdbc updateclasstime = new Jdbc(update);

						} catch (ClassNotFoundException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
			}
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		String alter = "alter table " + table + " drop column " + date + "";
		try {
			Jdbc j = new Jdbc(alter);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// checkdate中的值也要删掉
		date = date.replace("日", "");
		date = date.replaceAll("年|月", "-");

		String delete = "delete from checkdate where classname='" + classname + "' and centername='" + centername
				+ "' and date='" + date + "' ";
		try {
			Jdbc j = new Jdbc(delete);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		request.getRequestDispatcher("showsuccess.jsp").forward(request, response);
	}

}
